package cc.rengu.igas.bomp.core.service.trans;

import cc.rengu.igas.bomp.common.constant.BompAppParamConstant;
import cc.rengu.igas.bomp.common.dao.BompMchntAcctSumMapper;
import cc.rengu.igas.bomp.common.dao.MchntAcctInfoMapper;
import cc.rengu.igas.bomp.common.dao.MchntBaseInfoMapper;
import cc.rengu.igas.bomp.common.dao.impl.BompMchntAcctSumMapperImpl;
import cc.rengu.igas.bomp.common.dao.impl.MchntAcctInfoMapperImpl;
import cc.rengu.igas.bomp.common.dao.impl.MchntBaseInfoMapperImpl;
import cc.rengu.igas.bomp.common.entity.MchntAcctDetail;
import cc.rengu.igas.bomp.common.entity.MchntAcctInfo;
import cc.rengu.igas.bomp.common.entity.MchntBaseInfo;
import cc.rengu.igas.bomp.common.enums.MchntStatusEnum;
import cc.rengu.igas.bomp.common.enums.RespCodeEnum;
import cc.rengu.igas.bomp.common.enums.SettleTypeEnum;
import cc.rengu.igas.bomp.core.service.base.ManageService;
import cc.rengu.igas.bomp.facade.request.MchntD0QueryRequest;
import cc.rengu.igas.bomp.facade.response.MchntD0QueryResponse;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.common.dao.SysParamMapper;
import cc.rengu.oltp.service.common.dao.impl.SysParamMapperImpl;
import cc.rengu.oltp.service.common.entity.SysParam;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.oltp.utility.util.DateUtil;
import cc.rengu.oltp.utility.util.StringUtil;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * Created by zhangxuran on 2020/03/30
 */
public class MchntD0QueryService extends ManageService {
    @Override
    protected Object bizBeforeCust() throws Exception {
        MchntD0QueryRequest mchntD0QueryRequest = new MchntD0QueryRequest();
        ConvertUtil.convertOutput(mchntD0QueryRequest);
        return mchntD0QueryRequest;
    }

    @Override
    protected BizResponse bizProcessCust(Object request) throws Exception {

        MchntD0QueryRequest mchntD0QueryRequest = (MchntD0QueryRequest) request;
        MchntD0QueryResponse mchntD0QueryResponse = new MchntD0QueryResponse();
        BizResponse<MchntD0QueryResponse> bizResponse = new BizResponse<>();
        bizResponse.setRspSysId(AppParamConstant.SYS_ID);
        bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
        bizResponse.setResult(mchntD0QueryResponse);

        /*获取法人行号信息*/
        String instId = mchntD0QueryRequest.getHeader().getInstId();
        /*获取用户标识*/
        String userId = mchntD0QueryRequest.getUserId();
        /*获取商户号信息*/
        String mchntNo = mchntD0QueryRequest.getMchntNo();
        /*必填性校验*/
        if (StringUtil.isEmptyOrNull(instId) || StringUtil.isEmptyOrNull(userId) || StringUtil.isEmptyOrNull(mchntNo)) {
            rglog.error("必填性校验失败!");
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        }
        SysParamMapper sysParamMapper = new SysParamMapperImpl();
        if (BompAppParamConstant.MCHNTNO_DEFAULT.equals(mchntNo)) {
            /*商户入驻时(商户号默认为0000000000000)校验法人行是否开通D0清算、是否开通提现*/
            SysParam sysParamD0 = sysParamMapper.selectSysParamByPrimaryKey(instId, BompAppParamConstant.MCHNT_PARAM_TYPE, BompAppParamConstant.ALLOW_MCHNT_D0_SETTLE_INST);
            if (null != sysParamD0) {
                mchntD0QueryResponse.setD0Flag(sysParamD0.getParamValue());
            } else {
                mchntD0QueryResponse.setD0Flag(BompAppParamConstant.FLAG_N);
            }
            SysParam sysParamBhD0 = sysParamMapper.selectSysParamByPrimaryKey(instId, BompAppParamConstant.MCHNT_PARAM_TYPE, BompAppParamConstant.ALLOW_MCHNT_INNER_BANK_CARD_D0_INST);
            if (null != sysParamBhD0) {
                mchntD0QueryResponse.setInnerChannelD0Flag(sysParamBhD0.getParamValue());
            } else {
                mchntD0QueryResponse.setInnerChannelD0Flag(BompAppParamConstant.FLAG_N);
            }
            SysParam sysParamTx = sysParamMapper.selectSysParamByPrimaryKey(instId, BompAppParamConstant.MCHNT_PARAM_TYPE, BompAppParamConstant.CASHWITHDRAWALFLAG);
            if (null != sysParamTx) {
                mchntD0QueryResponse.setCashDrawFlag(sysParamTx.getParamValue());
            } else {
                mchntD0QueryResponse.setCashDrawFlag(BompAppParamConstant.FLAG_N);
            }
        } else {
            /*根据商户查询商户基本信息*/
            MchntBaseInfoMapper mchntBaseInfoMapper = new MchntBaseInfoMapperImpl();
            MchntBaseInfo mchntBaseInfo = mchntBaseInfoMapper.selectMchntBaseInfoByPrimaryKey(instId, mchntNo);
            if (null == mchntBaseInfo) {
                rglog.error("商户基本信息查询失败，商户号<{}>!", mchntNo);
                throw new BizException(RespCodeEnum.MCHNT_INFO_NOT_FOUND.getRespCode(), RespCodeEnum.MCHNT_INFO_NOT_FOUND.getRespDesc());
            }

            mchntD0QueryResponse.setD0Flag(BompAppParamConstant.FLAG_N);
            mchntD0QueryResponse.setInnerChannelD0Flag(BompAppParamConstant.FLAG_N);
            mchntD0QueryResponse.setCashDrawFlag(BompAppParamConstant.FLAG_N);

            if (MchntStatusEnum.UNNORMAL.getStatus().equals(mchntBaseInfo.getMchntStatus())) {
                rglog.debug("非正式商户,默认都不允许.");
                SysParam sysParamExpendDay = sysParamMapper.selectSysParamByPrimaryKey(instId, BompAppParamConstant.MCHNT_PARAM_TYPE, BompAppParamConstant.ALLOW_MCHNT_D0_EXPEND_DAY);
                if (null != sysParamExpendDay && AppParamConstant.YES.equals(sysParamExpendDay.getParamStatus())) {
                    if ("0".equals(sysParamExpendDay.getParamValue())) {
                        SysParam sysParamTransDay = sysParamMapper.selectSysParamByPrimaryKey(instId, BompAppParamConstant.MCHNT_PARAM_TYPE, BompAppParamConstant.ALLOW_MCHNT_D0_TRANS_DAY);
                        if (null != sysParamTransDay && AppParamConstant.YES.equals(sysParamTransDay.getParamStatus())) {
                            if ("0".equals(sysParamTransDay.getParamValue())) {

                            }
                        }
                    }

                }

                return bizResponse;
            }

            //原本是否已开通实时清算及D0
            if (AppParamConstant.YES.equals(mchntBaseInfo.getRealSettleFlag())) {
                rglog.debug("商户已开通实时清算,不进行校验.");
                mchntD0QueryResponse.setInnerChannelD0Flag(AppParamConstant.YES);
            } else {
                MchntAcctInfoMapper mchntAcctInfoMapper = new MchntAcctInfoMapperImpl();
                List<MchntAcctInfo> mchntAcctInfoList = mchntAcctInfoMapper.selectMchntAcctInfoByMchntNo(mchntBaseInfo.getInstId(), mchntBaseInfo.getMchntNo());
                if (!CollectionUtils.isEmpty(mchntAcctInfoList)) {
                    for (MchntAcctInfo mchntAcctInfo : mchntAcctInfoList) {
                        if (SettleTypeEnum.D0_TYPE.getSettleCode().equals(mchntAcctInfo.getSettleType())) {
                            rglog.debug("商户已开通D0结算,不进行校验.");
                            mchntD0QueryResponse.setD0Flag(AppParamConstant.YES);
                            break;
                        }
                    }
                }
            }

            //原本是否已开通提现
            if (AppParamConstant.YES.equals(mchntBaseInfo.getCashWithdrawalFlag())) {
                rglog.debug("商户已开通提现,不进行校验.");
                mchntD0QueryResponse.setCashDrawFlag(AppParamConstant.YES);
            }

            //商户入驻时间
            String mchntExpandTime = mchntBaseInfo.getMchntExpandTime();
            /*入驻时间格式转换*/
            mchntExpandTime = DateUtil.dateStringFormatConvert(mchntExpandTime, "yyyy-MM-dd HH:mm:ss.SSS", "yyyyMMdd");
            /*判断入驻时间是否超过（包含）90天 */
            SysParam sysParamExpendDay = sysParamMapper.selectSysParamByPrimaryKey(instId, BompAppParamConstant.MCHNT_PARAM_TYPE, BompAppParamConstant.ALLOW_MCHNT_D0_EXPEND_DAY);
            int num = 90;
            if (null != sysParamExpendDay) {
                String day = sysParamExpendDay.getParamValue();
                num = Integer.parseInt(day);
            }
            int dayNum = DateUtil.compareDate(mchntExpandTime, DateUtil.getFutureDay(-num));
            if (dayNum <= 0) {
                /*获取商户由当前日期前推30天是有每天有交易*/
                SysParam sysParamTransDay = sysParamMapper.selectSysParamByPrimaryKey(instId, BompAppParamConstant.MCHNT_PARAM_TYPE, BompAppParamConstant.ALLOW_MCHNT_D0_TRANS_DAY);
                int numDay = 30;
                if (null != sysParamTransDay) {
                    String day = sysParamTransDay.getParamValue();
                    numDay = Integer.parseInt(day);
                }
                BompMchntAcctSumMapper bompMchntAcctSumMapper = new BompMchntAcctSumMapperImpl();
                List<MchntAcctDetail> mchntTransList = bompMchntAcctSumMapper.selecBompMchntAcctSumByMchntNoAndLimDate(instId, mchntNo, DateUtil.getFutureDay(-numDay), DateUtil.getCurrentDate());
                if (mchntTransList.size() >= numDay) {
                    /**商户连续30天有交易*/
                    SysParam sysParamD0 = sysParamMapper.selectSysParamByPrimaryKey(instId, BompAppParamConstant.MCHNT_PARAM_TYPE, BompAppParamConstant.ALLOW_MCHNT_D0_SETTLE_INST);
                    if (null != sysParamD0 && AppParamConstant.YES.equals(sysParamD0.getParamStatus())) {
                        mchntD0QueryResponse.setD0Flag(sysParamD0.getParamValue());
                    }
                    SysParam sysParamInnerChannelD0 = sysParamMapper.selectSysParamByPrimaryKey(instId, BompAppParamConstant.MCHNT_PARAM_TYPE, BompAppParamConstant.ALLOW_MCHNT_INNER_BANK_CARD_D0_INST);
                    if (null != sysParamInnerChannelD0 && AppParamConstant.YES.equals(sysParamInnerChannelD0.getParamStatus())) {
                        mchntD0QueryResponse.setInnerChannelD0Flag(sysParamInnerChannelD0.getParamValue());
                    }
                    SysParam sysParamCashDraw = sysParamMapper.selectSysParamByPrimaryKey(instId, BompAppParamConstant.MCHNT_PARAM_TYPE, BompAppParamConstant.CASHWITHDRAWALFLAG);
                    if (null != sysParamCashDraw && AppParamConstant.YES.equals(sysParamCashDraw.getParamStatus())) {
                        mchntD0QueryResponse.setCashDrawFlag(sysParamCashDraw.getParamValue());
                    }
                }
            }
        }
        return bizResponse;
    }

    @Override
    protected void bizAfterCust(Object response) throws Exception {
        BizResponse bizResponse = (BizResponse) response;
        MchntD0QueryResponse mchntD0QueryResponse = (MchntD0QueryResponse) bizResponse.getResult();
        ConvertUtil.convertInput(mchntD0QueryResponse);
    }
}
